package com.sc.mockweipo.config;

import com.sc.mockweipo.service.LogListener;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author sc
 * Created on  2017/12/22
 *
 * 日志配置
 */
@Configuration
public class LogRabbitConfig extends BaseRabbitConfig {

    public static final String LOG_QUEUE = "LOG_QUEUE";
    public static final String LOG_EXCHANGE = "LOG_EXCHANGE";
    public final static String ROUTING_KEY="log";
    @Bean
    public Queue logQueue(){
        return new Queue(LOG_QUEUE);
    }
    /**
     * 监听容器
     */
    @Bean
    public SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, LogListener logListener){
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames(new String[]{LOG_QUEUE});
        container.setMessageListener(logListener);
        return container;
    }
    @Bean
    LogListener logListener(){
        return new LogListener();
    }

    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange(LOG_EXCHANGE);
    }

    @Bean
    Binding binding(Queue logQueue,DirectExchange directExchange){
        return BindingBuilder.bind(logQueue).to(directExchange).with(ROUTING_KEY);
    }
}
